<?php
	// No direct access to this file
	$info = $_SESSION['__default']['user'];
	
?>
<?php 
	$url = $this->baseurl.'/images/com_retailshop/';	
	$caregory = '';
/*	for($i = 0 ; $i < count($this->catagorys_Menu); $i++){
		if($i!=3)
			$caregory .= '<li><a href="index.php?option=com_retailshop&view=home&task=detailscatelog&id='.$this->catagorys_Menu[$i]->id.'">'.$this->catagorys_Menu[$i]->name.'</a></li>';
		else{
			$caregory .= '<li class="last"><a href="index.php?option=com_retailshop&view=home&task=detailscatelog&id='.$this->catagorys_Menu[$i]->id.'">'.$this->catagorys_Menu[$i]->name.'</a></li>';
			break;
		}
	}*/			
	if(isset($_GET['myshop']) && $this->shopinfo->id != $_GET['myshop']){
		header('Location: index.php?option=com_retailshop&view=myshop');
	}
	$edit = false;
	if(isset($this->shopinfo)){
		$edit = true;
	}
?>	
<div class="wrapper wmain">
<div class="content">
	<?php if(isset($this->exists)) { ?>
		<div id="main-search">
             <?php echo $this->loadTemplate('left');?>	
		</div>
		<div id="main-premium" class="product blockwhite">
			<p>You have a shop, if you want to open a new shop to register a new account!</p>
			<p><a href="index.php?option=com_retailshop&view=myshop&shop_id=<?php echo $this->myshop?>&true">Your shop!</a></p>
		</div>
	<?php }else { ?>
	<div id="nav">
		<h2>
			<?php echo $edit ? 'Change profile': 'Start Retail Shop' ; ?>
		</h2>
        <ul>			
			<li><a href="#retaishop-add-general">Product general</a></li>
			<li><a href="#retaishop-add-logo">Logo & Image</a></li>
			<li><a href="#retaishop-add-google">Google Map</a></li>
			<li><a href="#retaishop-add-adress">Other adress</a></li>
        </ul>
	</div>
	<div id="manager">
	<form id="adminForm" name="adminForm" action="index.php?option=com_retailshop" method="post" enctype="multipart/form-data">
	<div id="toolbar-box">
              <div class="toolbar">
                <ul>
					<li class="toolbar-register" id="register"><a href="#"><span class="icon-register"></span>Register</a></li>
					<li class="toolbar_cancel"><a href="#"><span class="icon-cancel"></span>Cancel</a></li>
                </ul>
              </div>
              <div class="title">
                <h2><?php echo $edit ? 'Change profile': 'Start Retail Shop' ; ?> Manager</h2>
              </div>
            </div>  
	<div id="retaishop-add-general">
		<div class="element">				
				<span id="err-startshop">					
				</span>
			</div>
		<div class="element">
			<label>Shop Category:</label>
			<select name="start-catelogy">
				<option value="-1">-- Choose Category --</option>
				<?php foreach($this->catagory as $cata){ ?>
				<option value="<?php echo $cata->id; ?>"><?php echo $cata->name; ?></option>
				<?php } ?>
			</select>
		</div>
		<div class="element">
			<label>Shop Name:</label>
			<input type="text" name="start-name" value="<?php echo $edit ? $this->shopinfo->name : '' ?>" />
		</div>
		<div class="element">
			<label>Title:</label>
			<input type="text" name="start-title" value="<?php echo $edit ? $this->shopinfo->title : '' ?>" />
		</div>
		<div class="element">
			<label>Link:</label>
			<input type="text" name="start-link" value="<?php echo $edit ? $this->shopinfo->link : '' ?>" />
		</div>
		<div class="element">
			<label>Short description:</label>
			<textarea name="start-short-description"><?php echo $edit ? $this->shopinfo->short_description : '' ?></textarea>
		</div>
		<div class="element">
			<label>Descitption:</label>
			<textarea name="start-description"><?php echo $edit ? $this->shopinfo->description : '' ?></textarea>
		</div>
		<div class="element">
			<label>Email:</label>
			<input type="text" name="start-email" value="<?php echo $edit ? $this->shopinfo->email : '' ?>" />
		</div>
		<div class="element">
			<label>Brand:</label>
			<input type="text" name="start-brand" />	 		
		</div>
<div><a href="#" id="nextstep_2" class="next" onClick="nextstep(this);"><span class="icon-register"></span>Next >></a></div>
	</div>	
	<div id="retaishop-add-logo">
		<div class="element">
			<label>Logo:</label>
			<input type="file" name="start-logo" />
		</div>
		<div class="element">
			<label>Image:</label>
			<input type="file" name="start-image" />
		</div>
		<?php if(!$edit) { ?>
		<div id="new-slideshow">
            <p>Slideshow 
				<button type="submit" id="btnaddnew">Add New Slider</button>
			</p>
        </div>
			<div id="slideimagess">
            	<div class="slide" id="slide1">
                	<h3>Slideshow <span>&or;</span></h3>
					<div style="display: none;" class="togglebox">
                        <div class="content">
                            <div class="element">
                  				<label>Title</label>
								<input type="text" name="slidetitle[]" value="" />
              				</div>
                			<div class="element">
								<label>Content</label>
								<textarea name="slidecontent[]"></textarea>
							</div>
							<div class="element">
								<label>Link</label>
								<input type="text" name="slidelink[]" value="" />
							</div>
							<div class="element">
								<label>Image</label>
								<input type="file" name="slideimage[]" value="" />
							</div>
							<div class="button-toggle">
								<a href="#" class="remove" >Remove</a>
								<a href="#" class="cancel">Cancel</a>
							</div>							
                        </div>
					</div>
                </div>			
			</div>
			<?php }else { 
				if($this->shopinfo->logo != ''){
					$logo = $url.'logo/'.$this->shopinfo->logo;
				}else{
					$logo = $url.'noimages.jpg';
				}
				if($this->shopinfo->image != ''){
					$image = $url.''.$this->shopinfo->image;
				}else{
					$image = $url.'noimages.jpg';
				}
			?>
				<div class="element">
					<div style="float:left">
					<h3>Preview logo</h3>
					<div>
						<img src="<?php echo $logo; ?>" alt="logo" width="200" />
					</div>
					</div>
					<div style="float:left; margin-left: 20px">
					<h3>Preview image</h3>
					<div>
						<img src="<?php echo $image; ?>" alt="image" width="200" />
					</div>
					</div>					
				</div>
				<div style="clear: both"></div>
			<?php } ?>
<div><a href="#" id="nextstep_3" class="next" onClick="nextstep(this);"><span class="icon-register"></span>Next >></a></div>
<div><a href="#" id="nextstep_1" class="prev" onClick="nextstep(this);"><span class="icon-register"></span><< Prev</a></div>
	</div>
	<div id="retaishop-add-google">
		<div class="element">
			<label>Google Map</label>
			<input type="checkbox" name="google-map" id="btngooglemap" 
				<?php if($edit) echo $this->shopinfo->isgooglemap ? 'checked' : ''; ?>/>
			<fieldset id="googlemapinfo" style="overflow-x: hidden; overflow-y: hidden; display: none;" ><legend> Google Map Info </legend>
				<div class="element">
					<label>Latitude</label>
					<input type="text" name="latitude" id="textfield" value="<?php echo $edit ? $this->shopinfo->latitude : ''?>"/>
				</div>
				<div class="element">
					<label>Longitude</label>
					<input type="text" name="longitude" id="textfield" value="<?php echo $edit ? $this->shopinfo->longitude : ''?>" />
				</div>
				<div class="element">
					<label>Adress</label>
					<input type="text" name="" id="textfield" value="<?php echo $edit ? $this->shopinfo->address_googlemap : ''?>" />
				</div>				
				<div class="element">
					<label>Title</label>
					<input type="text" name="" id="textfield"  value="<?php echo $edit ? $this->shopinfo->title_googlemap : ''?>" />
				</div>
				<div class="element">
					<label>Content</label>
					<textarea name=""><?php echo $edit ? $this->shopinfo->content_googlemap : ''?></textarea>
				</div>
			</fieldset>			
		</div>
<div><a href="#" id="nextstep_4" class="next" onClick="nextstep(this);"><span class="icon-register"></span>Next >></a></div>	
<div><a href="#" id="nextstep_2" class="prev" onClick="nextstep(this);"><span class="icon-register"></span><< Prev</a></div>
	</div>
	<div id="retaishop-add-adress">		
		<div class="element">
			<button type="button" id="addnewaddress">Add new</button>
		</div>
		<div class="element">
			<table width="100%">
				<thead>
					<tr>
						<th width="40%"><a href="#">Adress</a></th>
						<th colspan="3" width="60%"><a href="#">Location</a></th>
					</tr>
				</thead>
				<tbody id="addaddres">
					<?php if(isset($this->addressAllShop) && count($this->addressAllShop)){	
						$i = 1;
						foreach($this->addressAllShop as $add){ ?>
					<tr>						
						<td><input type="text" name="adress-address[]"  value="<?php echo $add->address; ?>"/></td>
						<td>
							<select class="select" id="country_select_<?php echo $i; ?>" onChange="loadCityForContry(this);" name="country_select[]">
								<option value="-1"> --- Select Country --- </option>
								<?php foreach($this->country as $c) { ?>
									<option value="<?php echo $c->id?>"><?php echo $c-> name; ?></option>          
								<?php } ?>
							</select>
						</td>
						<td>
							<select class="select" id="city_select_<?php echo $i; ?>" name="city_select[]">
								<option value="-1"> --- Select State --- </option>
							</select>
							<?php echo '<input type="hidden" name="id_edit[]" value="'.$add->id .'" />'; ?>
						</td>
						<td><input type="text" name="txtcity[]" placeholder="Input City ..." value="<?php echo $add->city; ?>" /></td>
						<script>
							$("Select[id='country_select_<?php echo $i; ?>'] option[value=<?php echo $add->country_id; ?>]").attr('selected', 'selected');	
							country_select(<?php echo $add->country_id; ?>,'city_select_<?php echo $i++; ?>', <?php echo $add->city_id; ?>);
						</script>						
					</tr>
					<?php } }else { ?>
					<tr>						
						<td><input type="text" name="adress-address[]"  /></td>
						<td>
							<select class="select" id="country_select_1" onChange="loadCityForContry(this);" name="country_select[]">
								<option value="-1"> --- Select Country --- </option>
								<?php foreach($this->country as $c) { ?>
									<option value="<?php echo $c->id?>"><?php echo $c-> name; ?></option>          
								<?php } ?>
							</select>
						</td>
						<td>
							<select class="select" id="city_select_1" name="city_select[]">
								<option value="-1"> --- Select State --- </option>
							</select>
						</td>			
						<td><input type="text" name="txtcity[]" placeholder="Input City ..." /></td>						
					</tr>	
					<?php } ?>					
				</tbody>
			</table>	
		</div>
		<?php if($edit && count($this->addressAllShop)) { 
			$i = 0;
		?>		
		<?php } ?>
<div><a href="#" id="nextstep_3" class="prev" onClick="nextstep(this);"><span class="icon-register"></span><< Prev</a></div>
	</div>	
		<input type="hidden" name="view" value="home" />
		<input type="hidden" name="task" value="saveshop" />		
		<?php echo $edit ? '<input type="hidden" name="action" value="edit" />' : ''; ?>
		<?php echo $edit ? '<input type="hidden" name="num" value="'.count($this->addressAllShop).'" />' : ''; ?>
		<?php echo $edit ? '<input type="hidden" name="idshop" value="'.$_GET['myshop'].'" />' : ''; ?>
		</form>
	</div>
	<?php } ?>
</div>   
</div>
<script type="text/javascript">
	<?php if(isset($this->addressAllShop)){ ?>
	var j = <?php echo count($this->addressAllShop) + 1; ?>;
	<?php }else { ?>
	var j = 2;
	<?php } ?>
		
	$('#addnewaddress').click(function(){
//		alert(j);
		var addadd = '<tr><td><input type="text" name="adress-address[]"></td><td><select name="country_select[]" onchange="loadCityForContry(this);" id="country_select_'+ j +'" class="select"><option value="-1"> --- Select Country --- </option>';	
		<?php foreach($this->country as $c) { ?>
			addadd = addadd + "<option value='<?php echo $c->id?>'><?php echo $c-> name; ?></option>";
		<?php } ?>		
		addadd = addadd + '</select></td><td><select class="select" id="city_select_'+ j +'" name="city_select[]"><option value="-1"> --- Selsect State --- </option></select></td><td><input type="text" name="txtcity[]" placeholder="Input City ..." /></td></tr>';

		$('#addaddres').append(addadd);
		j = j + 1;
	});
	$(".content #nav ul").idTabs();
	$('#register').click(function() {
		var shop_cate = $('select[name="start-catelogy"]').val();
		var shop_name = $('input:text[name="start-name"]').val();
		var shop_title = $('input:text[name="start-title"]').val();
		var shop_link = $('input:text[name="start-link"]').val();
		var shop_short_description = $('textarea[name="start-short-description"]').val();
		var shop_email = $('input:text[name="start-email"]').val();
		
		var shop_logo = $('input:file[name="start-logo"]').val();
		var shop_image = $('input:file[name="start-image"]').val();
		
		var shop_adress = $('input:text[name="adress-address[]"]').val();
		var err = '';
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;		
		var numberPattern = /^\d{10}$/;
		if(shop_cate==-1){
			err += '<p>please choose catelogy</p>';
		}
		if(shop_name==''){
			err += '<p>please enter name</p>';
		}
		if(shop_title==''){
			err += '<p>please enter title</p>';
		}
		if(shop_adress==''){
			err += '<p>please enter address</p>';
		}
		if(shop_link=='' || shop_email=='' || !emailPattern.test(shop_email)){
			err += '<p>please enter correct link and email</p>';
		}else{
			$.ajax({
			 url:"index.php?option=com_retailshop&view=home&task=checkmaillink",
			 data:{shop_link:shop_link, shop_email:shop_email},
			 type: "POST",
			 dataType: "html",
			 success:function(data) {
				if(data != 0) {
					err += '<p>This link or email is already registered</p>';
				}else   {
					//alert(data);
				}
			}
			});
		}
		var extension = shop_logo.split('.').pop().toLowerCase();
		if(shop_logo != '')
			if( $.inArray(extension, ['png', 'gif', 'jpeg', 'jpg']) == -1){
				err +='<p>please choose logo image</p>';
			}
		extension = shop_image.split('.').pop().toLowerCase();
		if( shop_image !='')
			if($.inArray(extension, ['png', 'gif', 'jpeg', 'jpg']) == -1){
			err +='<p>please choose image</p>';
		}
		
		
		if(err.length != 0){		
			$('#err-startshop').html(err);	
			$("a[href='#retaishop-add-general']").click();			
			return false;
		}else{
			$('#err-startshop').html('');
		}		
		
		$('#adminForm').submit();
	});
	function nextstep(id){		
		var arr = $(id).attr('id').split('_');
		var step = arr[1];			
		switch(step){			
			case '1':				
				$("a[href='#retaishop-add-general']").click();
				break;
			case '2':
				$("a[href='#retaishop-add-logo']").click();
				break;
			case '3':
				$("a[href='#retaishop-add-google']").click();
				break;
			case '4':
				$("a[href='#retaishop-add-adress']").click();				
				break;
			default:
				$("a[href='#retaishop-add-info']").click();
				break;
				
		}		
		return	false;
	}		
	var leaght = 10;
	$("#btnaddnew").click(function(){		
		leaght += 1;
		var content = '<div class="slide" id="lengthSlider_'+leaght+'"><h3>Slideshow <span>&or;</span></h3><div style="display: none;" class="togglebox"><div class="content"><div class="element"><label>Title</label><input type="text" name="slidetitle[]" value="" /></div><div class="element"><label>Content</label><textarea name="slidecontent[]"></textarea></div><div class="element"><label>Link</label><input type="text" name="slidelink[]" value="" /></div><div class="element"><label>Image</label><input type="file" name="slideimage[]" value="" /></div><div class="button-toggle"><a href="#" class="remove" class="btn-lengthSlider" id="slide_'+leaght+'">Remove</a><a href="#" class="cancel">Cancel</a></div></div></div></div>';	
		$('#slideimagess').after(content);
		
		return false;
	});
	
	$('a[id^="slide_"]').live('click',function(){		
		var arr = $(this).attr('id').split('_');		
		$('#lengthSlider_'+arr[1]).remove();
		return false;
	});
</script>
<?php if($edit){ ?>
 <script type="text/javascript">
    $(window).load(function() {		
		var sele = <?php echo $this->shopinfo->category_id; ?>;	
		$("Select[name='start-catelogy'] option[value='"+sele+"']").attr('selected', 'selected');
    });
 </script>
 <?php } ?>